package com.example.anliku.service.serviceImpl;

import com.example.anliku.entity.*;
import com.example.anliku.entity.System;
import com.example.anliku.mapper.TeamMemberMapper;
import com.example.anliku.mapper.TeamMemberNewMapper;
import com.example.anliku.mapper.UserMapper;
import com.example.anliku.service.TeamMemberService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import com.example.anliku.mapper.TeamMapper;
import com.example.anliku.service.TeamService;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

@Service
public class TeamServiceImpl implements TeamService{

    @Resource
    private TeamMapper teamMapper;
    @Resource
    private UserMapper userMapper;
    @Resource
    private TeamMemberMapper teamMemberMapper;
    @Resource
    private TeamMemberNewMapper teamMemberNewMapper;

    @Override
    public Message deleteByPrimaryKey(Integer teamId) {
        teamMemberNewMapper.deleteByTeamNewId(teamId);
        return teamMapper.deleteByPrimaryKey(teamId)>0?Message.success(null):Message.fail(null);
    }

    @Override
    public int insert(Team record) {
        return teamMapper.insert(record);
    }

    @Override
    public Message insertSelective(Team record) {
        return teamMapper.insertSelective(record)>0?Message.success(null).add(record.getTeamId()):Message.fail(null);
    }

    @Override
    public Message selectByPrimaryKey(Integer teamId) {
        return Message.success(null).add(teamMapper.selectByPrimaryKey(teamId));
    }

    @Override
    public Message updateByPrimaryKeySelective(Team record) {
        return teamMapper.updateByPrimaryKeySelective(record)>0?Message.success(null):Message.fail(null);
    }

    @Override
    public int updateByPrimaryKey(Team record) {
        return teamMapper.updateByPrimaryKey(record);
    }

    @Override
    public Message selectAll() {
        return Message.success(null).add(teamMapper.selectAll());
    }

    @Override
    public Message insertTeamIdByTeamMemberId(Integer teamId, String teamMemberId) {
        return teamMapper.insertTeamIdByTeamMemberId(teamId,teamMemberId)>0?Message.success(null):Message.fail(null);
    }

    @Override
    public Message deleteByTeamIdByTeamMemberId(Integer teamId, String teamMemberId) {
        return teamMapper.deleteByTeamIdByTeamMemberId(teamId,teamMemberId)>0?Message.success(null):Message.fail(null);
    }

    @Override
    public Message selectMemberAll() {
        List<Map> mapList = new LinkedList<>();
        List<Team> teamList = teamMapper.selectAll();
        for(Team team :teamList){
            HashMap map =new HashMap();
            map.put("team",team);
            List<User>userList =new LinkedList<>();
            String[] ids =team.getTeamMemberId().split(";");
            if (ids.length>0){
                for (String ID :ids){
                    User user= userMapper.selectByPrimaryKey(ID);
                    userList.add(user);
                }
            }
            map.put("userList", userList);
            mapList.add(map);
        }
        return Message.success(null).add(mapList);
    }

    @Override
    public Message selectMemberByTeamId(Integer teamId, @RequestParam Integer pageNum, @RequestParam Integer size) {
        PageHelper.startPage(pageNum,size);
        Team team =teamMapper.selectByPrimaryKey(teamId);
        List<User> userList = new LinkedList<>();
        String[] ids1 =team.getTeamMemberId().split(";");
        if (ids1.length>0){
            for (String ID :ids1){
                User user =userMapper.selectByPrimaryKey(ID);
                userList.add(user);
            }
        }
        PageInfo pageInfo = new PageInfo(userList);
        pageInfo.getTotal();
        return Message.success(null).add(pageInfo);
}

    @Override
    public Message selectteamByteamMemberId(String teamMemberId) {
        return null;
    }

    @Override
    public Message selectteamByteamMainId(String teamMainId) {
        return Message.success(null).add(teamMapper.selectteamByteamMainId(teamMainId));
    }

    @Override
    public Message selectAllByTeamMemberUserRealname(String teamMemberUserRealname) {
        List<Map> mapList = new LinkedList<>();
        List<User> userList=userMapper.selectByUserRealname(teamMemberUserRealname);
        for(User user:userList){
            String userRealname=user.getUserRealname();
            HashMap map = new HashMap();
            List<TeamMember> teamMemberList=teamMemberMapper.selectAllByTeamMemberUserRealname(userRealname);
            map.put("teamMemberList",teamMemberList);
            mapList.add(map);
        }
        return Message.success(null).add(mapList);
    }

    @Override
    public Message selectAllByTeamId(Integer teamId) {
        return Message.success(null).add(teamMemberNewMapper.selectAllByTeamNewId(teamId));
    }



}
